﻿using SqlSugar;

namespace drinking.Models
{
    /// <summary>
    /// 门店实体类
    /// </summary>
    /// 
    [SugarTable("Store")]
    public class Store
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int StoreId { get; set; }

        /// <summary>
        /// 门店编码
        /// </summary>
        public string StoreCode { get; set; }

        /// <summary>
        /// 登录密码（后台和上位机）
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 门店名称
        /// </summary>
        public string StoreName { get; set; }


        /// <summary>
        /// 网页登录需要用到的token
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// 门店状态	0： 正常  10：设备不通（上位机和设备） 20：心跳不通（上位机和服务器）
        /// </summary>
        public int StoreStatus { get; set; }

        /// <summary>
        /// 营业状态	0： 营业  10：歇业 由门店管理员设置
        /// </summary>
        /// 
        public int BusinessStatus { get; set; }

        /// <summary>
        /// 门店地址 经度
        /// </summary>
        public decimal Longitude { get; set; }

        /// <summary>
        /// 门店地址 维度
        /// </summary>
        public decimal Latitude { get; set; }

        /// <summary>
        /// 门店二维码图片url
        /// </summary>
        public string ErCodeUrl { get; set; }


        /// <summary>
        /// 辅料1 存量状态   在售： 0， 售空： 10
        /// </summary>
        public int AccessoryStatus1 { get; set; }
        public int AccessoryStatus2 { get; set; }
        public int AccessoryStatus3 { get; set; }
        public int AccessoryStatus4 { get; set; }


        /// <summary>
        /// 辅料价格设定值
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public List<decimal> AccessoryPrices { get; set; } = new List<decimal>() { 1, 1, 1, 1 };


        /// <summary>
        /// 店铺在售饮品
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public IEnumerable<IGrouping<string, StoreDrink>> StoreDrinkList { get; set; }



        /// <summary>
        /// 设备心跳时间
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public DateTime HeartbeatTime { get; set; }

        [SugarColumn(IsIgnore = true)]
        private int CurrOrderCode { get; set; } = 1;

        [SugarColumn(IsIgnore = true)]
        private string LastCreateCodeDate { get; set; }

        /// <summary>
        /// 生成订单编码 ,生成规则 同一门店，每天刷新
        /// </summary>
        /// <returns></returns>
        public string CreateOrderCode()
        {
            lock(this)
            {
                string nowDate = DateTime.Now.ToString("yyyyMMdd");
                if (nowDate != LastCreateCodeDate || CurrOrderCode == 999)
                {
                    LastCreateCodeDate = nowDate;
                    CurrOrderCode = 1;
                }
            }
            return $"W{CurrOrderCode:000}";
        }


        [SugarColumn(IsIgnore = true)]
        private int CurrOrderItemCode { get; set; } = 1;

        [SugarColumn(IsIgnore = true)]
        private string LastCreateItemCodeDate { get; set; }

        internal string CreateOrderItemCode()
        {
            lock (this)
            {
                string nowDate = DateTime.Now.ToString("yyyyMMdd");
                if (nowDate != LastCreateItemCodeDate || CurrOrderItemCode == 999)
                {
                    LastCreateItemCodeDate = nowDate;
                    CurrOrderItemCode = 1;
                }
            }
            return $"W{CurrOrderItemCode:000}";
        }

        public (bool canPay, string errMsg) DrinkCheck(OrderItem item)
        {
            foreach (var storeDrinkGroup in StoreDrinkList)
            {
                var storeDrink = storeDrinkGroup.Single(sd => sd.StoreDrinkId == item.StoreDrinkId && sd.StoreDrinkStatus == 0);

                if (storeDrink != null)
                {
                    if (storeDrink.StoreDrinkStatus != 0)
                    {
                        return (false, $"{storeDrink._Drink.DrinkName}已停售，请重新点单");
                    }
                    
                    if (item.Accessory1 != 0 && this.AccessoryStatus1 != 0)
                    {
                        return (false, "库存发生了变化，请重新点单");
                    }
                    if (item.Accessory2 != 0 && this.AccessoryStatus2 != 0)
                    {
                        return (false, "库存发生了变化，请重新点单");
                    }
                    if (item.Accessory3 != 0 && this.AccessoryStatus3 != 0)
                    {
                        return (false, "库存发生了变化，请重新点单");
                    }
                    if (item.Accessory4 != 0 && this.AccessoryStatus4 != 0)
                    {
                        return (false, "库存发生了变化，请重新点单");
                    }
                    item.BasePrice = item.CupMode == "大杯" ? storeDrink._Drink.LargePrice : storeDrink._Drink.StandardPrice;
                    item.Accessory1 = item.Accessory1 == 0 ? 0 : AccessoryPrices[0];
                    item.Accessory2 = item.Accessory2 == 0 ? 0 : AccessoryPrices[0];
                    item.Accessory3 = item.Accessory3 == 0 ? 0 : AccessoryPrices[0];
                    item.Accessory4 = item.Accessory4 == 0 ? 0 : AccessoryPrices[0];
                    item.AccessoryPrice = item.Accessory1 + item.Accessory2 + item.Accessory3 + item.Accessory4;
                    item.Price = item.BasePrice + item.AccessoryPrice;

                    item.DrinkId = storeDrink._Drink.DrinkId;
                    item.DrinkName = storeDrink._Drink.DrinkName;
                    item.DrinkImage = storeDrink._Drink.DrinkImage;
                    return (true, "");
                }
            }
            return (false, "库存发生了变化，请重新点单");
        }
    }
}
